package com.lovelyj.trip_by_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovelyj.trip_by_train.common.R;
import com.lovelyj.trip_by_train.entity.DailyRemainTicket;
import com.lovelyj.trip_by_train.entity.DailySeat;
import com.lovelyj.trip_by_train.service.DailyRemainTicketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/dailyRemainTicket")
public class DailyRemainTicketController {

    @Autowired
    private DailyRemainTicketService dailyRemainTicketService;

    @GetMapping("/select")
    public R<Page<DailyRemainTicket>> select(HttpServletRequest request, Integer page , String trainNum, String travelDate) {
        log.info(trainNum+" "+travelDate);
        Page<DailyRemainTicket> pageInfo=new Page<>(page,10);
        LambdaQueryWrapper<DailyRemainTicket> queryWrapper = new LambdaQueryWrapper<DailyRemainTicket>();
        queryWrapper.eq(DailyRemainTicket::getTrainNum, trainNum);
        queryWrapper.eq(DailyRemainTicket::getDate, LocalDate.parse(travelDate));
        dailyRemainTicketService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @PostMapping("/search")
    public R<List<DailyRemainTicket>> search(HttpServletRequest request, @RequestBody Map<String,Object> search){
        log.info(search.toString());
        DailyRemainTicket dailyRemainTicket=new DailyRemainTicket();
        if(search.get("startStation")!="")
            dailyRemainTicket.setStartStation(search.get("startStation").toString());
        if(search.get("endStation")!="")
            dailyRemainTicket.setEndStation(search.get("endStation").toString());
        if(search.get("startTime")!="")
            dailyRemainTicket.setStartTime(LocalDateTime.parse(search.get("startTime").toString().replace("Z","")));
        LambdaQueryWrapper<DailyRemainTicket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(DailyRemainTicket::getStartTime, LocalDateTime.now().plusHours(2));
        if (dailyRemainTicket.getStartTime() != null) {
            queryWrapper.ge(DailyRemainTicket::getStartTime,dailyRemainTicket.getStartTime().plusHours(8));
        }
        if (dailyRemainTicket.getStartTime() != null) {
            queryWrapper.lt(DailyRemainTicket::getStartTime,dailyRemainTicket.getStartTime().plusHours(8).plusDays(1));
        }
        queryWrapper.like(dailyRemainTicket.getStartStation()!=null,DailyRemainTicket::getStartStation,dailyRemainTicket.getStartStation());
        queryWrapper.like(dailyRemainTicket.getEndStation()!=null,DailyRemainTicket::getEndStation,dailyRemainTicket.getEndStation());

        List<DailyRemainTicket> list=dailyRemainTicketService.list(queryWrapper);

        return R.success(list);
    }

    @GetMapping("/getTrainInfo")
    public R<DailyRemainTicket> getTrainInfo(HttpServletRequest request,Long id){

        LambdaQueryWrapper<DailyRemainTicket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyRemainTicket::getId,id);
        DailyRemainTicket dailyRemainTicket = dailyRemainTicketService.getOne(queryWrapper);
        return R.success(dailyRemainTicket);

    }

}
